class Solution {
public:
    int findTheLongestBalancedSubstring(string s) {
        int ans = 0;
        int n = s.size();
        int f[2] = { 0 };
        for (int i = 0; i < n; i++) {
            if (s[i] == '0') {
                f[0]++;
                f[1] = 0;
            }
            else {
                f[1]++;
                ans = max(ans, 2 * min(f[1], f[0]));
                if (i + 1 < n && s[i + 1] == '0')f[0] = 0;
            }
        }
        return ans;
    }
};